package com.care.wardmate.server.service.impl;

import com.care.common.core.result.PageResult;
import com.care.common.security.utils.SecurityUtils;
import com.care.wardmate.common.domain.Article;
import com.care.wardmate.common.request.ArticleRequest;
import com.care.wardmate.server.mapper.ArticleMapper;
import com.care.wardmate.server.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @author 33253
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageResult<Article> selectArticle(ArticleRequest articleRequest) {
        PageHelper.startPage(articleRequest.getPageNum(),articleRequest.getPageSize());
        List<Article> articleList = articleMapper.selectArticle(articleRequest);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        return PageResult.toPageResult(pageInfo.getTotal(),articleList);
    }
    @Override
    public void insertArticleByUserId(Article article) {
        Long userId = SecurityUtils.getUserId();
        article.setUserId(Math.toIntExact(userId));
        article.setArticleTime(new Date());
        articleMapper.insertArticleByUserId(article);
    }
    @Override
    public Article selectArticleById(Integer articleId) {
        Article article = articleMapper.selectArticleById(articleId);
        return article;
    }
}
